package com.smartStatus.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartStatus.admin.service.SysDictDetailsService;
import com.smartStatus.admin.service.SysDictService;
import com.smartStatus.api.entity.SysDict;
import com.smartStatus.api.entity.SysDictDetails;
import com.smartStatus.api.entity.TenantGrade;
import com.smartStatus.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName : SysDictController
 * @Author : lby
 * @Date: 2020/12/16 14:42
 * @Description : 系统字典管理
 */
@RestController
@RequestMapping("/dict")
@AllArgsConstructor
@Slf4j
@Api("系统字典管理")
public class SysDictController {

    private final SysDictService sysDictService;
    private final SysDictDetailsService sysDictDetailsService;

    /**
     * 字典分页数据
     *
     * @param page
     * @param sysDict
     * @return
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @ApiOperation("字典分页数据")
    @GetMapping("/page")
    public R dictPages(Page<SysDict> page, SysDict sysDict) {
        return sysDictService.getDictPage(page,sysDict);
    }

    /**
     * 字典明细
     *
     * @param sysDict
     * @return
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @ApiOperation("字典明细")
    @PostMapping("/detail")
    public R getDetails(@RequestBody SysDict sysDict) {
        return R.ok(sysDictDetailsService.getDetails(sysDict));
    }

    /**
     * 添加字典
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @ApiOperation("添加字典")
    @PostMapping("/save")
    public R saveDict(@RequestBody SysDict sysDict) {
        return R.ok(sysDictService.saveDict(sysDict));
    }

    /**
     * 修改字典
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @PostMapping("/edit")
    @ApiOperation("修改字典")
    public R editDict(@RequestBody SysDict sysDict) {
        return R.ok(sysDictService.editDict(sysDict));
    }

    /**
     * 删除字典
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @PostMapping("/del")
    @ApiOperation("删除字典")
    public R delDict(@RequestBody Integer[] ids) {
        SysDict sysDict = new SysDict();
        for (int i = 0; i < ids.length; i++) {
            sysDict.setId(ids[i]);
            sysDictService.delDict(sysDict);
        }
        return R.ok();
    }

    /**
     * 添加字典项
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @PostMapping("/save-details")
    @ApiOperation("添加字典项")
    public R saveDetails(@RequestBody SysDictDetails sysDictDetails) {
        sysDictDetailsService.saveDetails(sysDictDetails);
        return R.ok(sysDictDetailsService.insertRevolation(sysDictDetails));
    }

    /**
     * 修改字典项
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @PostMapping("/edit-details")
    @ApiOperation("修改字典项")
    public R editDetails(@RequestBody SysDictDetails sysDictDetails) {
        return R.ok(sysDictDetailsService.editDetails(sysDictDetails));
    }

    /**
     * 删除字典项及其关系
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @PostMapping("/del-details")
    @ApiOperation("删除字典项及其关系")
    public R delRevo(@RequestBody SysDictDetails sysDictDetails) {
        sysDictDetailsService.delRevolation(sysDictDetails);
        return R.ok(sysDictDetailsService.delDetails(sysDictDetails));
    }

    /**
     * 测试权限
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @GetMapping("/test-permission")
    @ApiOperation("测试权限")
    public R testPermission() {
        return R.ok("对不起，没有权限");
    }

    /**
     * 根据字典项名称查询具体内容
     */
    @PreAuthorize("@pms.hasPermission('sys_dict_view')")
    @PostMapping("/get-dict-byName")
    @ApiOperation("根据字典项名称查询具体内容")
    public R getDictByName(@RequestBody String dictName) {
        Map dataMap = (Map) JSON.parse(dictName);
        if (StrUtil.isNotEmpty(dataMap.get("dictName").toString())) {
            SysDictDetails sysDict = new SysDictDetails();
            sysDict.setDictName(dataMap.get("dictName").toString());
            QueryWrapper<SysDictDetails> query = Wrappers.query();
            query.eq("dict_name", dataMap.get("dictName").toString());
            return R.ok(sysDict.selectList(query));
        } else {
            return R.failed("参数错误，请重试");
        }

    }

}
